package ru.adhoc.truealarmfree.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;

import ru.adhoc.truealarmfree.R;
import ru.adhoc.truealarmfree.barcodreader.CameraTestActivity;
import ru.adhoc.truealarmfree.time.TimeHelper;
import ru.adhoc.truealarmfree.utils.Util;

/**
 * Created by Малюгин Сергей on 11.10.13.
 */
public class CancelAlarmBarcode extends Activity {

    private int id;
    private Context context;
    private final int REQUEST_CODE_SCAN = 1;
    private LinearLayout llParentBar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.cancel_alarm_barcode);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        id = getIntent().getExtras().getInt("id");
        context = this.getApplicationContext();

        //google analytics
        if(getResources().getBoolean(R.bool.analytics_enable))
            EasyTracker.getInstance().setContext(context);

        llParentBar = (LinearLayout)findViewById(R.id.ll_parent_bar);
        Util.getInstance(context).setTheme(llParentBar);

    }

    public void pressResetAlarmBarcode(View view){

        int maxRepeat = getSharedPreferences("pref", MODE_PRIVATE).getInt("maxRepeat", 15);
        TimeHelper.getInstance(context).currentRepeat =
                TimeHelper.getInstance(context).currentRepeat + 1;

        if(TimeHelper.getInstance(context).currentRepeat <= maxRepeat){
            Util.getInstance(context).resetAlarmUtil(id);
            this.finish();
        }
        else
        {
            Toast.makeText(context, getResources()
                    .getText(R.string.maxRepeatOverflow).toString(), Toast.LENGTH_LONG).show();
        }
    }

    public void pressCancelAlarmBarcode(View view){
        Intent CameraTestActivity = new Intent(context, CameraTestActivity.class);
        CameraTestActivity.putExtra("AlarmCancel", true);
        startActivityForResult(CameraTestActivity, REQUEST_CODE_SCAN);
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_SCAN:
                    boolean barcodeScanned = data.getBooleanExtra("barcodeScanned", false);
                    if(barcodeScanned){
                        Util.getInstance(context).cancelAlarmUtil(id);
                        this.finish();
                    }
                    break;
            }
        }
        else {
            Toast.makeText(this, "Wrong result", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_settings, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=ru.adhoc.truealarm&referrer=utm_source%3Dclick_in_freeapp"));
        startActivity(intent);
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Util.getInstance(context).setTheme(llParentBar);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(getResources().getBoolean(R.bool.analytics_enable))
            EasyTracker.getInstance().activityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(getResources().getBoolean(R.bool.analytics_enable))
            EasyTracker.getInstance().activityStop(this);
    }
}